package cn.edu.zafu.bigdata.x.wupeng.g;

import java.io.IOException;
import java.io.PipedReader;
import java.io.PipedWriter;

public class PipeDemo2 {
    public static void main(String[] args) {
        new PipeDemo2().Test();
    }
    public void Test(){
        try{
            PipedReader prd = new PipedReader();
            PipedWriter pwr = new PipedWriter(prd);

            Wtr wtr = new Wtr(pwr);
            Rdr rdr = new Rdr(prd);

            wtr.start();
            rdr.start();

        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    class Wtr extends Thread{
        private PipedWriter writer;

        public Wtr(PipedWriter pwr){
            writer = pwr;
        }

        @Override
        public void run(){
            String s = "好好学习，天天向上";
            char[] chr = s.toCharArray();
            System.out.println("Send "+chr.length+" Bytes : "+s);
            try {
                writer.write(chr,0,chr.length);
            } catch (IOException e) {
                e.printStackTrace();
            }
            System.out.println("Send done");

        }


    }
    class Rdr extends Thread{
        private PipedReader reader;

        public Rdr(PipedReader prd){
            reader = prd;
        }

        @Override
        public void run(){

            char[] chr = new char[1024];
            int len = 0;
            try {
                len = reader.read(chr,0,1024);

            } catch (IOException e) {
                e.printStackTrace();
            }
            System.out.println("read "+len+"Bytes : "+new String(chr));

        }
    }
}
